package net.godideas.giframework.widget;

import net.godideas.giframework.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.style.ScaleXSpan;
import android.util.AttributeSet;
import android.widget.TextView;

public class CTextView extends TextView {
	
	private float mLetterSpacing = 0;
    private String mOriginalText = "";
	
    /**
     * Get letter spacing
     * @return
     */
    public float getLetterSpacing() {
        return mLetterSpacing;
    }

    /**
     * Set letter spacing
     * @param value
     */
    public void setLetterSpacing(float value) {
        this.mLetterSpacing = value;
        applyLetterSpacing();
    }
    
	/**
	 * Set font
	 * @param font
	 */
	public void setFont(String font) {
		Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + font + ".ttf");				    
	    this.setTypeface(typeface);
	}
	
	@Override
	public void setText(CharSequence text, BufferType type) {
		if (type == BufferType.NORMAL) mOriginalText = "";
		super.setText(text, type);
		applyLetterSpacing();
	}
		
	public CTextView(Context context) {
		super(context);
		init(null);
	}
	public CTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs);
	}
	public CTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(attrs);
	}
	
	/**
	 * Initialize control
	 * @param attrs
	 */
	private void init(AttributeSet attrs)
	{
		if(attrs != null){
			parseAttributes(attrs);
		}
	}
	
	/**
	 * Parse attributes
	 * @param attrs
	 */
	@SuppressLint("Recycle")
	private void parseAttributes(AttributeSet attrs) {
		TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CTextView);
		
		if (a.hasValue(R.styleable.CTextView_font)){
			String font = a.getString(R.styleable.CTextView_font);
			setFont(font);
		}
		if (a.hasValue(R.styleable.CTextView_letterSpacing)){
			setLetterSpacing(a.getFloat(R.styleable.CTextView_letterSpacing, 0));
		}
	}	
	
	/**
	 * Apply letter spacing to this text view
	 */
	private void applyLetterSpacing() {
		if (mOriginalText.length() <= 0) mOriginalText = getText().toString();
		
        StringBuilder builder = new StringBuilder();
        for(int i = 0; i < mOriginalText.length(); i++) {
            builder.append(mOriginalText.charAt(i));
            if(i+1 < mOriginalText.length()) {
                builder.append(" ");
            }
        }
        SpannableString finalText = new SpannableString(builder.toString());
        if(builder.toString().length() > 1) {
            for(int i = 1; i < builder.toString().length(); i+=2) {
            	float scale = mLetterSpacing / 10;
                finalText.setSpan(new ScaleXSpan(scale), i, i+1, 0);
            }
        }
        super.setText(finalText, BufferType.SPANNABLE);
    }
}